/**
 * 
 */
package org.swing.table.filter.util;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.table.TableCellEditor;

import org.swing.table.filter.InlineFilterHeader;

/**
 * @author lqnhu
 *
 */
public class DragHeaderMouseListener extends MouseAdapter implements MouseMotionListener {
	private int x;
	private int y;
	private final InlineFilterHeader inlineFilterHeader;

	public DragHeaderMouseListener(InlineFilterHeader inlineFilterHeader) {
		this.inlineFilterHeader = inlineFilterHeader;
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	}

	@Override
	// this avoid dragging the renderer while the editor stays, so hide editor
	public void mouseDragged(MouseEvent e) {
		TableCellEditor tableCellEditor = this.inlineFilterHeader
				.getTableCellEditor();
		int dist = Math.abs(this.x - e.getX()) + Math.abs(this.y - e.getY());
		if (tableCellEditor != null
				&& dist >= this.inlineFilterHeader.getStopEditingDragDistance()) {
			tableCellEditor.stopCellEditing();
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		this.x = e.getX();
		this.y = e.getY();
	}
}
